package com.mawell.common.propertymapperswing;

import org.apache.log4j.Logger;

import javax.swing.JTable;
import javax.swing.table.*;
import java.util.*;

/**
 * JTable that maps a List object to a table.
 * Allows for simple editing of properties.
 *
 * @author David Ekholm (author of JMapTable that was transformed into this)
 * @see java.util.Map
 */
public class JListTable extends JTable {

    private static Logger logger = Logger.getLogger(JListTable.class);

    private boolean editable = true;

    public void setCellEditable(boolean editable) {
        this.editable = editable;
    }

    class ListModel extends AbstractTableModel {

        public String colName = "Value";

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return editable;
        }

        public String getColumnName(int i) {
            return colName;
        }

        public int getColumnCount() {
            return 1;
        }

        public int getRowCount() {
            return items.size() + 1; // One empty last row
        }

        public Object getValueAt(int row, int col) {
            if(row >= items.size()) {
                return "";
            }
            return items.get(row);
        }

        public void setValueAt(Object aValue, int row, int col) {
            if(row >= items.size()) { // Sometimes we can really get larger row than items.size()
                items.add(aValue);
            } else {
                items.set(row, aValue);
            }
            fireTableDataChanged();
        }
    }

    private Vector items = new Vector();
    private ListModel theModel = new ListModel();

    public JListTable() {
        super.setModel(theModel);
        super.setRowSelectionAllowed(false);
    }

    public JListTable(String nameLabel) {
        setHeaderName(nameLabel);
        super.setModel(theModel);
        super.setRowSelectionAllowed(false);
    }

    public void setHeaderName(String nameLabel) {
        theModel = new ListModel();
        theModel.colName = nameLabel;
        super.setModel(theModel);
    }

    public void clear() {
        items.clear();
    }

    public List getList() {
        TableCellEditor editor = getCellEditor();
        if(editor != null) {
            editor.stopCellEditing(); // An editing is being performed. Notify model

        }
        ArrayList theList = new ArrayList();
        for(int i = 0; i < items.size(); i++) {
            Object value;
            if(items.get(i) instanceof IDItem) {
                value = ((IDItem)items.get(i)).id;
            } else {
                value = items.get(i);
            }

            if(value != null && !value.toString().equals("")) {
                theList.add(value);
            }
        }

        return theList;
    }

    public void addAll(List theList) {
        Iterator it = theList.iterator();
        while(it.hasNext()) {
            items.add(it.next());
        }
    }

    public void add(Object value) {
        items.add(value);
    }
}